package com.farm.bpm.form.impl;

import java.util.List;


import com.farm.bpm.form.Field;
import com.farm.bpm.form.FormParseInter;
import com.farm.doc.domain.FarmDocfile;
import com.farm.doc.server.FarmFileManagerInter;
import com.farm.web.spring.BeanFactory;

public class FileFormParse implements FormParseInter {
	private final static FarmFileManagerInter fileIMP = (FarmFileManagerInter) BeanFactory
			.getBean("farm_docFileProxyId");

	@Override
	public void saveHandle(String fieldType, List<String> parameter,
			String name, Field field) {
		if (!"file".equals(fieldType)) {
			return;
		}
		if (field.getValue() != null) {
			fileIMP.submitFile(field.getValue());
		}
	}

	@Override
	public Field parse(String fieldType, List<String> parameter, String name,
			Field field) {
		if (!"file".equals(fieldType)) {
			return null;
		}
		StringBuffer sb = new StringBuffer();
		FarmDocfile file = null;
		sb.append("<input  readonly=\"readonly\" type=\"text\"");
		sb.append(" style='width: " + field.getFormWidth() + "px;'");
		sb.append(" class='easyui-validatebox'");
		if (field.getValue() != null) {
			file = fileIMP.getFile(field.getValue());
			if (file != null) {
				sb.append(" value='" + file.getName() + "'");
			}
		}
		if (field.isRequired()) {
			sb.append(" data-options=\"required:true\"");
		}
		sb.append(" id='" + field.getId() + "file' >");
		sb.append(" <input type=\"button\" ");
		sb.append(" id=\"" + field.getId() + "button\" value=\"上传附件\" />");
		sb.append(" <input type=\"hidden\"  ");
		sb.append(" id=\"" + field.getId() + "\" ");
		sb.append(" value='" + field.getValue() + "' ");
		sb.append(" name='" + field.getName() + "'>");
		sb.append("<script type=\"text/javascript\"> ");
		sb.append("  $(function() {");
		sb.append("    var uploadbutton" + field.getId()
				+ " = KindEditor.uploadbutton( {");
		sb.append("      button : KindEditor('#" + field.getId()
				+ "button')[0],");
		sb.append("      fieldName : 'taskFile',");
		sb.append("      url : 'admin/uploadtaskFile.do',");
		sb.append("      afterUpload : function(data) {");
		sb.append("        if (data.error === 0) { ");
		sb.append("          $('#" + field.getId()
				+ "file').val(data.taskFileFileName);");
		sb.append("          $(\"#" + field.getId() + "\").val(data.id);");
		sb.append("        } else {");
		sb.append("          if (data.message) {");
		sb.append("              alert(data.message);");
		sb.append("           } else {");
		sb.append("      	    alert('请检查文件格式');");
		sb.append("          }");
		sb.append("        }");
		sb.append("      },");
		sb.append("      afterError : function(str) {");
		sb.append("    	    alert('自定义错误信息: ' + str);");
		sb.append("      }");
		sb.append("    });");
		sb.append("    uploadbutton" + field.getId()
				+ ".fileBox.change(function(e) {uploadbutton" + field.getId()
				+ ".submit();});");
		sb.append("  });");
		sb.append("</script> ");
		field.setFielsHtmlEdit(sb.toString());
		if (file != null) {
			field.setFielsHtmlView("<a href=\"admin/loadTaskfile.do?id="
					+ field.getValue() + "\">下载文件("
					+ fileIMP.getFile(field.getValue()).getName() + ")</a>");
		}
		return field;
	}

	@Override
	public String getfieldTypeDescribes() {
		return "扩展类型:附件上传";
	}

	@Override
	public String getfieldTypeFormat() {
		return "name=\"名称#{'type':'file'}\" type=\"string\"";
	}
}
